package day24.edu;

import day20.edu.Person;
import org.junit.Test;

import java.time.LocalDate;
import java.util.*;

// Created: 24/09/2025, ChangJiang Ru
public class MapTest {
    @Test
    public void test1()
    {
        Map map = new HashMap();
        map.put(null, null);

        System.out.println(map);
    }

    @Test
    public void test2()
    {
        Map map = new Hashtable();
//        map.put(null, 123);
        map.put(123, null);
        System.out.println(map);
    }
    
    @Test
    public void test3()
    {
        HashMap map = new HashMap();
        map.put("AA", 23);
        map.put(45, "Tom");
        map.put(new Date(), 12);
        map.put(new Person("Jack", 89),"Tom");

        System.out.println(map);
        System.out.println(map.size());

        Object value = map.remove(45);
        System.out.println(value);

        Object oldValue = map.put("AA", 24);
        System.out.println(oldValue);

        Object value1 = map.get("AA");
        System.out.println(value1);

        System.out.println(map);

        Set keySet = map.keySet();
        System.out.println(keySet);
        // Case 1:
        Iterator iterator = keySet.iterator();
        while(iterator.hasNext())
        {
            Object key = iterator.next();
            System.out.println(key);
        }
        // Case 2:
        for (Object obj : keySet)
        {
            System.out.println(obj);
        }

        Collection values = map.values();
        // Case 1: Use the iterator
        Iterator iterator1 = values.iterator();
        while(iterator1.hasNext())
        {
            Object obj = iterator1.next();
            System.out.println(obj);
        }

        System.out.println("==========");
        // Case 2: Use the stronger loop
        for (Object obj : values)
        {
            System.out.println(obj);
        }

        System.out.println("=========== The Case 3 =======");
        // Case 3: Use the get() method
        Set keySet1 = map.keySet();
        for (Object key : keySet1)
        {
            Object value2 = map.get(key);
            System.out.println(value2);
        }


    }

    @Test
    public void test4()
    {
        HashMap map = new HashMap();

        map.put("AA", 23);
        map.put("BB", 45);
        map.put(new Person("Jonathan Blow", 45), "Game Programmer");
        map.put(new Person("Tosding", 30), "Linux programmer");
        map.put(LocalDate.now(), "is now time");

        Set entrySet = map.entrySet();
        Iterator iterator = entrySet.iterator();
        while(iterator.hasNext())
        {
            Map.Entry entry = (Map.Entry)iterator.next();
            System.out.println(entry.getKey() + " ---------> " + entry.getValue());
        }
    }
}
